package com.example.walkera.rtsp_android_client.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.widget.Toast;

import com.example.walkera.rtsp_android_client.R;
import com.example.walkera.rtsp_android_client.function.ActivityCollector;
import com.example.walkera.rtsp_android_client.function.CameraInfoSharedPreferencesHelper;
import com.example.walkera.rtsp_android_client.function.WifiCheck;
import com.example.walkera.rtsp_android_client.global.GlobalInfo;
import com.example.walkera.rtsp_android_client.rtspclient.RtspClient;

import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback,RtspClient.RtspEventHandler{

    private final static String TAG="MainActivity";
    public static final int CONNECT_SUCCESS = 0;
    public static final int CONNECT_FAIL = 1;
    public static final int CONNECT_RETRY = 2;
    public static final int CONNECT_NEW_WIFI_SUCCESS = 3;
    public static final int CONNECT_NEXT_WIFI = 4;
    public static final int NO_WIFI_CONNECTTED = 5;
    public static final int CONNECTING = 6;

    private List<ScanResult> mwifiListResult = null;
    private int mReconnectTime = 0;
    private static final int RETRY_TIMES = 6;
    private SurfaceView msurfaceView;
    private RtspClient  mRtspClient;
    private HashMap<String,String>mMapRtspUrl=new HashMap<>();
    private ProgressDialog mProgressDialog=null;
    private WifiCheck mWifiCheck=null;
    //如果作为home,则不能退出
//    private static final boolean HomeVersion=false;
    private static final boolean HomeVersion=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这种去掉标题栏的方法取不到，需要到mainifest里面去掉
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        GlobalInfo.getInstance().setCurrentApp(this);
        //先添加飞机连接url的信息
        mMapRtspUrl.put("VITUS","rtsp://192.168.1.235:554/");
        mMapRtspUrl.put("AIBAO","rtsp://192.168.1.1/H264?W=720&H=400&BR=1500000&FPS=30");
        mMapRtspUrl.put("VOYAGER4","rtsp://192.168.1.235/stream");

        //创建一个存储的用的信息文件
        CameraInfoSharedPreferencesHelper.getInstance().createCameraInfoTable(this);

//        if (mWifiCheck==null)
//            mWifiCheck=new WifiCheck();

        //获取视频渲染用的view
        msurfaceView=(SurfaceView)findViewById(R.id.video_view);
        msurfaceView.getHolder().addCallback(this);

        //add by lihaiping1603@aliyun.com on 2016-12-26
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //test for rtsp
//        mRtspClient=new RtspClient("rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov");
//        mRtspClient=new RtspClient("rtsp://192.168.1.1/H264?W=720&H=400&BR=1500000&FPS=30");
//        mRtspClient=new RtspClient("rtsp://192.168.1.235:554/");
//        mRtspClient=new RtspClient(this,"rtsp://192.168.1.235/stream");
//        mRtspClient.setSurfaceView(msurfaceView);
//        mRtspClient.start();
//        return;
//
        String url=null;
        String strCameraTypeName=CameraInfoSharedPreferencesHelper.getInstance().readAirPlaneName();
        if (strCameraTypeName.isEmpty()){//如果读出来的name为空，则弹出type选择页面
            Intent intent=new Intent(MainActivity.this,AirplaneChoose.class);
            startActivity(intent);
        }
        else{
            if (WifiCheck.isWifiContected(this)) {//如果wifi连接了的话
                url = mMapRtspUrl.get(strCameraTypeName);
                if(url==null){
                    Intent intent1=new Intent(MainActivity.this,AirplaneChoose.class);
                    startActivity(intent1);
                }else{
                    mRtspClient = new RtspClient(this,url);
                    mRtspClient.setSurfaceView(msurfaceView);
                    mRtspClient.start();
                }
            }
            else{//如果未连接
                Intent intent1=new Intent(MainActivity.this,AirplaneChoose.class);
                startActivity(intent1);
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mRtspClient!=null)
            mRtspClient.shutdown();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_1://搜索
                //启动wifi链接页面
                Intent intent=new Intent(MainActivity.this,LHPWifiConnectActivity.class);
                startActivity(intent);
                break;
            case KeyEvent.KEYCODE_2://退出
                if(!HomeVersion)
                    ActivityCollector.finishAll();
                break;
            case KeyEvent.KEYCODE_6://确认
                //启动机型配置页面
                Intent intent1=new Intent(MainActivity.this,AirplaneChoose.class);
                startActivity(intent1);
                break;
            default:
                return super.onKeyDown(keyCode, event);
        }
        return true;
    }

    @Override
    public void rtspNetConnectFailed(RtspClient client,String strFailedReason){
        Toast.makeText(MainActivity.this, R.string.connect_camera_failed, Toast.LENGTH_LONG).show();
        //启动机型配置页面
        Intent intent1=new Intent(MainActivity.this,AirplaneChoose.class);
        startActivity(intent1);
    }
    @Override
    public void rtspCmdStateCallBack(RtspClient client,String cmd,String strState){
        if (strState.compareTo("200")!=0){//如果不是回复状态200，说明失败了
            //关闭流
            if(client!=null)
                client.shutdown();
        }
    }

    public static boolean getHomeVervison() {
        return HomeVersion;
    }
    //
//    private Handler mMainHandler=new Handler(){
//        public void handleMessage(Message msg){
//            Log.d(TAG, "handMassage: what= "+msg.what);
//            switch (msg.what){
//                case CONNECT_SUCCESS:
//                break;
//                case CONNECTING:
//                    if (mProgressDialog==null) {
//                        mProgressDialog = new ProgressDialog(MainActivity.this);
//                        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//                        mProgressDialog.setMessage(MainActivity.this.getString(R.string.action_processing));
//                        mProgressDialog.setCancelable(false);
//                        mProgressDialog.show();
//                    }
//                    break;
//            }
//        }
//    };

}
